<?php

namespace app\service;


use app\tool\SingletonTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


class TaskRepairReportService
{
    use SingletonTrait;

    /**
     * @title 查询返修情况
     * 返回 任务返修类型、返修时间、返修状态
     * @param array  $projectIds 需要查询的项目id，以int类型数组形式
     * @param array  $stepCategoryIds 需要查询的环节id，以int类型数组形式
     * @param array  $timeScope 时间范围，二元int数组形式，传递时间戳，结构为 [开始时间, 结束时间]
     * @param array  $page 分页参数，二元int数组形式，结构为 [页码, 页面大小]
     * @param string $queryType 查询模式
     * @return array|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTaskRepairList(array $projectIds, array $stepCategoryIds = [], array $timeScope = [], array $page = [], $queryType = "default")
    {
        // 判断必要条件
        if (empty($projectIds)) {
            return [];
        }
        //组装查询条件
        $dbQuery = (new \app\model\TaskRepairOrderModel);
        $filterList = [
            "task_repair_order.project_id" => [
                "type" => "in",
                "value" => $projectIds
            ], // 指定项目
            "task_repair_order.step_category_id" => [
                "type" => "in",
                "value" => $stepCategoryIds
            ], // 指定环节
            "task_repair_order.created" => [
                "type" => "between",
                "value" => $timeScope
            ], // 指定时间范围
            "pagination" => [
                "type" => "pagination",
                "value" => $page
            ] // 指定分页
        ];
        foreach ($filterList as $field => $value) {
            if (!empty($value["value"])) {
                switch ($value["type"]) {
                    case "in":
                        $dbQuery = $dbQuery->whereIn($field, $value["value"]);
                        break;
                    case "between":
                        $dbQuery = $dbQuery->whereBetween($field, $value["value"]);
                        break;
                    case "pagination":
                        $dbQuery = $dbQuery->page(...$page);
                        break;
                }
            }
        }
        switch ($queryType) {
            // 统计
            case "count":
                $repairResult = [
                    "count" => $dbQuery->count()
                ];
                break;
            case "count_by_month":
                // 按月统计
                $repairResult = $dbQuery
                    ->field([
                        "from_unixtime(task_repair_order.created, '%Y-%m') as time",
                        "count(distinct task_repair_order.id) as count",
                    ])
                    ->group("from_unixtime(task_repair_order.created, '%Y-%m')")
                    ->select()
                    ->toArray();
                break;
            case "count_by_day":
                // 按日统计
                $repairResult = $dbQuery
                    ->field([
                        "from_unixtime(task_repair_order.created, '%Y-%m-%d') as time",
                        "count(distinct task_repair_order.id) as count",
                    ])
                    ->group("from_unixtime(task_repair_order.created, '%Y-%m-%d')")
                    ->select()
                    ->toArray();
                break;
            default:
            case "list":
                // 列表查询
                $repairResult = $dbQuery
                    ->field([
                        "task_repair_order.id as task_repair_order_id",
                        "task_repair_order.task_id",
                        "task_repair_order.step_category_id",
                        "task_repair_order.report_type",
                    ])
                    ->select()
                    ->toArray();
                break;
        }
        return $repairResult;
    }

}
